package com.bustle.product.data.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.Query;

import com.bustle.commons.data.dao.Dao;
import com.bustle.product.data.Product;
import com.bustle.product.data.Product.ProductType;

@Local
public interface ProductDao extends Dao<Integer, Product>
{
  public List<Product> retrieveProductsByTypeAndProvider(ProductType productType, Integer idProvider);
  public List<Product> retrieveProductsByProductType(ProductType productType);
  public List<Product> retrieveProductsByIdProvider(int idProvider);

  @Stateless
  public class DefaultProductDao extends ProductUnitDao<Integer, Product> implements ProductDao
  {
    public DefaultProductDao()
    {
      super(Product.class);
    }

    @Override
    public List<Product> retrieveProductsByTypeAndProvider(ProductType productType, Integer idProvider)
    {
      String paramQuery = "SELECT p FROM Product p WHERE p.productType = :paramProductType AND p.provider.idProvider = :paramIdProvider";
      Query query = entityManager.createQuery(paramQuery);
      query.setParameter("paramProductType", productType);
      query.setParameter("paramIdProvider", idProvider);

      @SuppressWarnings("unchecked")
      List<Product> result = query.getResultList();
      System.out.println("longueur de la liste " + result.size());
      return result;
    }

    @Override
    public List<Product> retrieveProductsByProductType(ProductType productType)
    {
      String paramQuery = "SELECT p FROM Product p WHERE p.productType = :paramProductType";
      Query query = entityManager.createQuery(paramQuery);
      query.setParameter("paramProductType", productType);

      @SuppressWarnings("unchecked")
      List<Product> result = query.getResultList();
      System.out.println("longueur de la liste " + result.size());
      return result;
    }

    @Override
    public List<Product> retrieveProductsByIdProvider(int idProvider)
    {
      String paramQuery = "SELECT p FROM Product p WHERE p.provider.idProvider = :paramIdProvider";
      Query query = entityManager.createQuery(paramQuery);
      query.setParameter("paramIdProvider", idProvider);

      @SuppressWarnings("unchecked")
      List<Product> result = query.getResultList();
      System.out.println("longueur de la liste " + result.size());
      return result;
    }


  }
}